package com.secsbrain.cmdb.web.controller;

import java.util.Date;
import java.util.List;

import com.secsbrain.sso.core.SsoHelper;
import com.secsbrain.cmdb.dal.model.BareTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.cmdb.biz.IBareTemplateService;
import com.secsbrain.common.ResponseResult;

/**
 * 服务器规格控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/bareTemplate")
public class BareTemplateController{
	
    @Autowired
    IBareTemplateService bareTemplateService;
    
    /**
   	 * @api {GET} /api/bareTemplate/list  查询服务器规格列表
   	 * 
   	 * @apiGroup HOST
   	 * 
   	 * @apiParam {String{20}}  [productname]  服务器型号
   	 * @apiParam {String{20}}  [name]         规格名称
   	 * 
   	 * @apiSuccess {Object[]} data            数据列表，具体字段请查看系分文档
   	 * 
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult<?> queryList(Integer page, Integer limit, BareTemplate params) {
    	if(StringUtils.isEmpty(params.getName())){
    		params.setName(null);
    	}
    	if(StringUtils.isEmpty(params.getProductname())){
    		params.setProductname(null);
    	}
    	Page<BareTemplate> pageBean=new Page<>(page, limit);
    	Page<BareTemplate> result=bareTemplateService.selectPage(pageBean, params);
    	return new ResponseResult(result);
    }
    
    /**
   	 * @api {GET} /api/bareTemplate/all  查询所有的服务器规格
   	 * 
   	 * @apiGroup BARE
   	 * 
   	 * @apiSuccess {Object[]} data            数据列表 
   	 * @apiSuccess {Object[]} data.name        规格名称，具体字段请查看系分文档
   	 * 
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public ResponseResult<?> queryAll() {
    	BareTemplate bareTemplateParam=new BareTemplate();
    	List<BareTemplate> result=bareTemplateService.selectList(bareTemplateParam);
    	return new ResponseResult(result);
    }
    
    /**
   	 * @api {POST} /api/bareTemplate/insert  新增服务器规格
   	 * 
   	 * @apiGroup HOST
   	 * 
   	 * @apiParam {String{20}}  name         规格名称，其他字段见系分
   	 * 
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult<?> insert(@RequestBody BareTemplate entity) {
    	entity.setCreateTime(new Date());
    	entity.setCreator(SsoHelper.currentUser().getNickname());
    	bareTemplateService.insertSelective(entity);
    	return new ResponseResult();
    }
    
    /**
   	 * @api {PUT} /api/bareTemplate/update  更新服务器规格
   	 * 
   	 * @apiGroup HOST
   	 * 
   	 * @apiParam {String{20}}  name         规格名称，其他字段见系分
   	 * 
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseResult<?> update(@RequestBody BareTemplate entity) {
    	entity.setUpdateTime(new Date());
    	entity.setUpdator(SsoHelper.currentUser().getNickname());
    	bareTemplateService.updateSelectiveById(entity);
    	return new ResponseResult();
    }
    
    /**
   	 * @api {DELETE} /api/bareTemplate/delete  删除服务器规格
   	 * 
   	 * @apiGroup HOST
   	 * 
   	 * @apiParam {Number}  id         主键
   	 * 
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult<?> delete(@RequestBody BareTemplate entity) {
    	bareTemplateService.deleteById(entity.getId());
    	return new ResponseResult();
    }
    
}
